//
//  CategoryFactory.cpp
//  QuizMaster
//
//  Created by Jeppe Nielsen on 3/22/15.
//  Copyright (c) 2015 Jeppe Nielsen. All rights reserved.
//

#include "CategoryFactory.hpp"
#include "QuestionComponent.hpp"
#include "AlignCategoriesSystem.hpp"
#include "MathHelper.hpp"
#include "Gui.hpp"
#include "SelectQuestionSystem.hpp"
#include "TransformAnimationDatabase.hpp"
#include "TransformAnimatorSystem.hpp"
#include "DraggableSystem.hpp"
#include <sstream>

void CategoryFactory::Initialize() {
    categoryDestroyerSystem = World()->CreateSystem<CategoryDestroyerSystem>();
    questionDestroyerSystem = World()->CreateSystem<QuestionDestroyerSystem>();
    World()->CreateSystem<DraggableSystem>();
}

void CategoryFactory::SetGui(Nano::Gui *gui, GameObject* font) {
    this->gui = gui;
    this->font = font;
}

void CategoryFactory::CreateCategory(Category &category) {

    GameObject* go = gui->CreatePivot(0,0);
    go->AddComponent<CategoryComponent>()->category = &category;
    go->AddComponent<Transform>();
    
    GameObject* frontSide = gui->CreateControl(go, "Background", 0, {2.0f, 0.5f});
    frontSide->GetComponent<Transform>()->Anchor = frontSide->GetComponent<Sizeable>()->Size * 0.5f;
    
    
    
    GameObject* name = gui->CreateLabel(frontSide, 0, frontSide->GetComponent<Sizeable>()->Size, font, category.name, 0.2f);
    name->GetComponent<Label>()->HAlignment = Font::Center;
    name->GetComponent<Label>()->VAlignment = Font::Middle;
    
    for (int i=0; i<category.questions.size(); i++) {
        CreateQuestion(go->GetComponent<CategoryComponent>(), *category.questions[i]);
    }
}

void CategoryFactory::CreateQuestion(CategoryComponent* category, Question &question) {
    
    Vector2 size(2.0f, 1.75f);
    
    GameObject* go = gui->CreatePivot(0, 0);
    go->AddComponent<QuestionComponent>()->category = category;
    go->GetComponent<QuestionComponent>()->question = &question;
    go->AddComponent<Transform>();
    //go->AddComponent<TransformAnimator>();
    
    category->questions.push_back(go);
    
    GameObject* frontSide = gui->CreateControl(go, "Background", 0, size);
    frontSide->GetComponent<Transform>()->Anchor = size * 0.5f;
    frontSide->AddComponent<QuestionComponent>(go);
    
    std::stringstream s;
    s<<(question.points);
    
    GameObject* name = gui->CreateLabel(frontSide, 0, size, font, s.str(), 0.5f);
    name->GetComponent<Label>()->HAlignment = Font::Center;
    name->GetComponent<Label>()->VAlignment = Font::Middle;
    name->GetComponent<Label>()->WordWrap = true;
    
    GameObject* backSide = gui->CreateControl(go, "Background", 0, size);
    backSide->GetComponent<Transform>()->Anchor = size * 0.5f;
    backSide->GetComponent<Transform>()->Rotation = Quaternion(ToRadians(180), {0,1,0});
    
    GameObject* questionLabel = gui->CreateLabel(backSide, 0, size, font, question.question, 0.1f);
    questionLabel->GetComponent<Label>()->HAlignment = Font::Center;
    questionLabel->GetComponent<Label>()->VAlignment = Font::Middle;
    questionLabel->GetComponent<Label>()->WordWrap = true;
    
}

void CategoryFactory::ClearAll() {
    categoryDestroyerSystem->DestroyAllCategories();
    questionDestroyerSystem->DestroyAllQuestions();
}

void CategoryFactory::CreateSelectableCategory(Category &category, GameObject* parent, Vector2 position) {

    GameObject* go = gui->CreatePivot(parent,position);
    go->AddComponent<SelectableCategory>()->category = &category;
    go->AddComponent<Transform>();
    go->AddComponent<Draggable>();
    
    GameObject* frontSide = gui->CreateControl(go, "Background", 0, {2.0f, 0.5f});
    frontSide->GetComponent<Transform>()->Anchor = frontSide->GetComponent<Sizeable>()->Size * 0.5f;
    frontSide->AddComponent<SelectableCategory>(go);
    go->AddComponent<Touchable>(frontSide);
    
    
    GameObject* name = gui->CreateLabel(frontSide, 0, frontSide->GetComponent<Sizeable>()->Size, font, category.name, 0.2f);
    name->GetComponent<Label>()->HAlignment = Font::Center;
    name->GetComponent<Label>()->VAlignment = Font::Middle;
}




